﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.UI;
using Emgu.Util;
using Emgu.CV.Structure;
using SignDetection;
namespace Plate
{
    public partial class SignDetectionForm : Form
    {
        Bitmap ori_Bitmap;

        Point DownPoint;

        ImageAttributes ImageAttributes;
        string FileName;
        public SignDetectionForm(string filename)
        {
            this.FileName = filename;
            InitializeComponent();
        }
        public SignDetectionForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.tabControl1.SelectedIndex = 0;
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "jpg files(*.jpg)|*.jpg|all files(*.*)|*.*";
            dlg.Multiselect = true;
            if (dlg.ShowDialog() != DialogResult.OK)
                return;
            if (dlg.FileNames.Length == 1)
            {
                ori_Bitmap = new Bitmap(dlg.FileName);
                pictureBox1.Image = ori_Bitmap;
                pictureBox1.Refresh();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.tabControl1.SelectedIndex = 1;
            SignDetection.SignDetector detector = SignDetection.SignDetector.CreateSignDetector();
            PlatePositionResult LV = detector.PlatePositioning(ori_Bitmap);
            List<Bitmap>  signs=detector.DetectSign(LV.Output);
            character1.Image = signs[0];
            character1.Refresh();
            character2.Image = signs[1];
            character2.Refresh();
            character3.Image = signs[2];
            character3.Refresh();
            character4.Image = signs[3];
            character4.Refresh();
            character5.Image = signs[4];
            character5.Refresh();
            character6.Image = signs[5];
            character6.Refresh();
            character7.Image = signs[6];
            character7.Refresh();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //SignDetection.SignDetector detector = SignDetection.SignDetector.CreateSignDetector();
            ////List<Bitmap> signs = detector.SegmentSigns(ori_Bitmap);//函数调用错误
            //List<Bitmap> signs = detector.DetectSign(ori_Bitmap);
            
        }

    

   private void buttonCharLineDetect_Click(object sender, EventArgs e)
   {
       this.tabControl1.SelectedIndex = 2;
       //Bitmap bitmap = binary(ori_Bitmap);
       //Bitmap LV = GetLV(bitmap, ori_Bitmap);
       //List<Bitmap> signs = Recognize(LV);
       //Bitmap vert_bit = Vert_DoubEdge(Binary_LV,3,25);
       //Bitmap hori_bit = Hori_DoubEdge(Binary_LV,3,25);
       //pictureBox_Hori.Image = hori_bit;
       //pictureBox_Hori.Refresh();
       //pictureBox_Vert.Image = vert_bit;
       //pictureBox_Vert.Refresh();

   }

   private void SignDetectionForm_Load(object sender, EventArgs e)
   {
       if (this.FileName != null)
       {
           this.ori_Bitmap = new Bitmap(this.FileName);
           this.pictureBox1.Image = this.ori_Bitmap;
           this.ImageAttributes = (new CoCarImages()).ReadImageAttributes(this.FileName);
           if (this.ImageAttributes == null)
               this.ImageAttributes = new ImageAttributes();
       }
   }

   bool InDrag = false;
   private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
   {
       DownPoint = new Point(e.X,e.Y);
       InDrag = true;
   }

   private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
   {
       Point MovePoint = new Point(e.X,e.Y);
       if (InDrag)
       {
           Graphics g = Graphics.FromImage(ori_Bitmap);
           g.DrawRectangle(new Pen(new SolidBrush(Color.Gray)), DownPoint.X, DownPoint.Y, MovePoint.X - DownPoint.X, MovePoint.Y - DownPoint.Y);
       }
   }
   void LoadImageAttributes()
   {
   }
   void SaveImageAttributes()
   {
       CoCarImages co = new CoCarImages();
       co.SaveImageAttributes(this.FileName, this.ImageAttributes);
   }

   private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
   {
       this.InDrag = false;
       if (Math.Abs(e.X - DownPoint.X) > 10 && Math.Abs(e.Y - DownPoint.Y) > 10)
       {
           int x=-1; int y=-1; int width=0, height=0;
           if(e.X<DownPoint.X)
           {
               x=e.X;width=DownPoint.X-e.X;
           }
           else
           {
               x=DownPoint.X;
               width=e.X-DownPoint.X;
           }
           if(e.Y<DownPoint.Y)
           {
               y=e.Y;width=DownPoint.Y-e.Y;
           }
           else
           {
               y=DownPoint.Y;
               height = e.Y - DownPoint.Y;
           }
           ImageAttributes.PlatePosition = new Rectangle(x, y, width, height);
           ImageAttributes.HasPlatePosition = true;
           this.SaveImageAttributes();
       }
   }

   private void pictureBox1_Paint(object sender, PaintEventArgs e)
   {
       if (this.ImageAttributes.HasPlatePosition)
       {
           e.Graphics.DrawRectangle(new Pen(Color.Gray), this.ImageAttributes.PlatePosition);
       }
   }

   }
}
       
